/*
 * ioctl.c
 *
 *  Created on: Mar 10, 2013
 *      Author: is4@uw.edu
 */

#include "ioctl.h"
#include "gfxctl.h"
#include "random.h"
#include <stdlib.h>

IoctlPtr ioctl_new(void)
{
	IoctlPtr ioctl = (IoctlPtr)malloc(sizeof(IoctlStr));
	ioctl->kbd = kbdctl_new();

	return ioctl;
}

void ioctl_initialize(IoctlPtr ioctl)
{
	kbdctl_initialize(ioctl->kbd);
}

void ioctl_free(IoctlPtr ioctl)
{
	kbdctl_free(ioctl->kbd);
	free(ioctl);
}

void ioctl_kbdkeypress(IoctlPtr ioctl, Byte character)
{
	kbdctl_writechar(ioctl->kbd, character);
}

Byte ioctl_read(IoctlPtr ioctl, Byte port)
{
	switch (port)
	{
		case IO_KBD_STATUS:
			return kbdctl_haschar(ioctl->kbd);

		case IO_KBD_DATA0:
			return kbdctl_readchar(ioctl->kbd);

		default:
			// All other ports hold 0.
			return 0;
	}
}

void ioctl_write(IoctlPtr ioctl, Byte port, Byte value)
{
	switch (port)
	{
		case IO_VID_DATA:
			// Write through synchronously.
			gfxctl_writechar(value);
			break;

		default:
			// Do nothing.
			break;
	}
}
